const app = getApp()
let qCloud = require("../../index.js");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        organName: "",
        realName: "",
        avatar: null,
        kindType: '',
        status: '0'
    },

    navigateTap: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let self = this;
        self.getUserInfo();

        let userInfo = qCloud.session.getUser();
        self.setData({
            organName: userInfo.organName,
            avatar: (userInfo.avatar == null || userInfo.avatar == "") ? null : userInfo.avatar,
            realName: qCloud.crypto.decrypt(userInfo.realNameEncrypt),
            kindType: userInfo.kindType,
            status: userInfo.status
        });
    },

    getUserInfo: function () {
        let self = this;
        app.fetchApis(self, qCloud.api.getInfo, {}, "GET", function (e) {
            if (e.data.code == 200) {
                qCloud.session.setPermi(e.data.permissions);
                qCloud.session.setRole(e.data.role);
                qCloud.session.setUser(e.data.user);
            }
        });
    },

    chooseImage() {
        let self = this;
        wx.chooseMedia({
            count: 1,
            mediaType: ["image"],
            sourceType: ["album", "camera"],
            maxDuration: 30,
            camera: "back",
            success(res) {
                wx.uploadFile({
                    url: qCloud.api.uploadAvatar, //仅为示例，非真实的接口地址
                    filePath: res.tempFiles[0].tempFilePath,
                    name: "avatarfile",
                    formData: {
                        "avatarfile": res.tempFiles[0].tempFilePath,
                        "fileType": "14"
                    },
                    header: qCloud.buildSessionHeader(),
                    success(res) {
                        let resJson = JSON.parse(res.data);
                        if (resJson.code == 200) {
                            self.setData({
                                avatar: resJson.data.avatar,
                            });

                            let userInfo = qCloud.session.getUser();
                            userInfo.avatar = resJson.data.avatar;
                            qCloud.session.setUser(userInfo);
                        }
                    },

                    fail(error) {
                        wx.showToast({
                            title: error,
                            icon: "error"
                        })
                    }
                })
            },
            fail(e) {

            }
        });
    },

    onShow: function () {
        this.getUserInfo();
        let userInfo = qCloud.session.getUser();
        this.setData({
            organName: userInfo.organName,
            avatar: (userInfo.avatar == null || userInfo.avatar == "") ? null : userInfo.avatar,
            realName: qCloud.crypto.decrypt(userInfo.realNameEncrypt),
            kindType: userInfo.kindType,
            status: userInfo.status
        });
    },

    onPullDownRefresh: function () {
        this.getUserInfo();

        let userInfo = qCloud.session.getUser();
        this.setData({
            organName: userInfo.organName,
            avatar: (userInfo.avatar == null || userInfo.avatar == "") ? null : userInfo.avatar,
            realName: qCloud.crypto.decrypt(userInfo.realNameEncrypt),
            kindType: userInfo.kindType,
            status: userInfo.status
        });
    },

    logOff: function () {
        let self = this;
        wx.showModal({
            title: '提示',
            content: '确定要注销账号吗？注销后无法恢复，请谨慎操作',
            success(res) {
                if (res.confirm) {
                    app.fetchApis(self, qCloud.api.accountLogOff, {}, "POST", function (e) {
                        if (e.data.code == 200) {
                            qCloud.session.clear();
                            let session = qCloud.session.get();
                            if (!session || !session.loginInfo && session.loginInfo.accessToken == "") {
                                wx.reLaunch({
                                    url: '/pages/login/index',
                                });
                            }
                        } else {
                            wx.showToast({
                                title: e.data.msg,
                                icon: "error"
                            })
                        }
                    });
                }
            }
        })
    },

    bindClickOutLogin: function () {
        let self = this;
        app.fetchApis(self, qCloud.api.logout, {}, "POST", function (e) {
            if (e.data.code == 200) {
                qCloud.session.clear();
                let session = qCloud.session.get();
                if (!session || !session.loginInfo && session.loginInfo.accessToken == "") {
                    wx.reLaunch({
                        url: '/pages/login/index',
                    });
                    return;
                } else {
                    wx.reLaunch({
                        url: '/pages/index/index',
                    });
                }
            }
        });
    }
})
